home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Draw / Clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  7.2 KB  |  241 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Clipboard.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the DTS.Draw
  20. ** clipboard document.  The clipboard document is simply a modified DTS.Draw document.
  21. ** Many of the main document facilities are removed, since they don't apply to the
  22. ** clipboard.  See ClipboardInitDocument() for a full breakdown of the changes in
  23. ** the document procedures. */
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for the application.        */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __TREEOBJ2__
  40. #include "TreeObj2.h"
  41. #endif
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. extern RgnHandle    gCursorRgn;
  50. extern CursPtr        gCursorPtr;
  51.  
  52. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window);
  53. static void        ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick);
  54. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough);
  55. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt);
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60. /*****************************************************************************/
  61.  
  62.  
  63.  
  64. /* Initialize the clipboard document. */
  65.  
  66. #pragma segment ClipboardDoc
  67. OSErr    ClipboardInitDocument(FileRecHndl frHndl)
  68. {
  69.     OSErr        err;
  70.     FileRecPtr    frPtr;
  71.  
  72.     err = DefaultInitDocument(frHndl, 0, 0, 0);
  73.         /* Use the hierarchical document architecture. */
  74.  
  75.     if (!err) {        /* Set some custom window behaviors, and disable the rest. */
  76.         frPtr = *frHndl;
  77.         frPtr->fileState.calcFrameRgnProc        = nil;
  78.         frPtr->fileState.contentClickProc        = ClipboardContentClick;
  79.         frPtr->fileState.contentKeyProc          = ClipboardContentKey;
  80.         frPtr->fileState.drawFrameProc           = nil;
  81.         frPtr->fileState.freeDocumentProc        = nil;
  82.         frPtr->fileState.freeWindowProc          = nil;
  83.         frPtr->fileState.initContentProc         = ClipboardInitContent;
  84.         frPtr->fileState.readDocumentProc        = nil;
  85.         frPtr->fileState.readDocumentHeaderProc  = nil;
  86.         frPtr->fileState.resizeContentProc       = nil;
  87.         frPtr->fileState.scrollFrameProc         = nil;
  88.         frPtr->fileState.undoFixupProc           = nil;
  89.         frPtr->fileState.windowCursorProc        = ClipboardWindowCursor;
  90.         frPtr->fileState.writeDocumentProc       = nil;
  91.         frPtr->fileState.writeDocumentHeaderProc = nil;
  92.     }
  93.  
  94.     return(err);
  95. }
  96.  
  97.  
  98.  
  99. /*****************************************************************************/
  100.  
  101.  
  102.  
  103. /* Initialize the clipboard document size.  By waiting this late to state the
  104. ** document size, the window is initially opened to the size described in the
  105. ** 'WIND' resource for the clipboard.  Once the window exists, we can then
  106. ** set the document size to be 7 inches by 10 inches. */
  107.  
  108. #pragma segment ClipboardDoc
  109. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window)
  110. {
  111. #ifndef __MWERKS__
  112. #pragma unused (window)
  113. #endif
  114.  
  115.     SetDocSize(frHndl, (7 * 72), (10 * 72));
  116.     return(noErr);
  117. }
  118.  
  119.  
  120.  
  121. /*****************************************************************************/
  122. /*****************************************************************************/
  123.  
  124.  
  125.  
  126. /* Handle only document scrolling for the clipboard. */
  127.  
  128. #pragma segment ClipboardDoc
  129. static void    ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  130. {
  131. #ifndef __MWERKS__
  132. #pragma unused (frHndl, firstClick)
  133. #endif
  134.  
  135.     IsCtlEvent(window, event, nil, nil);
  136. }
  137.  
  138.  
  139.  
  140. /*****************************************************************************/
  141.  
  142.  
  143.  
  144. /* No keys for the clipboard.  Returning true eats all of the keys, so that
  145. ** they aren't passed on to the next window behind the clipboard. */
  146.  
  147. #pragma segment ClipboardDoc
  148. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  149. {
  150. #ifndef __MWERKS__
  151. #pragma unused (window, event, passThrough)
  152. #endif
  153.  
  154.     return(true);
  155. }
  156.  
  157.  
  158.  
  159. /*****************************************************************************/
  160.  
  161.  
  162.  
  163. /* Whenever the clipboard is active, just use an arrow cursor. */
  164.  
  165. #pragma segment ClipboardDoc
  166. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  167. {
  168. #ifndef __MWERKS__
  169. #pragma unused (frHndl, window, globalPt)
  170. #endif
  171.  
  172.     SetCursor(gCursorPtr = &qd.arrow);
  173.     return(true);
  174. }
  175.  
  176.  
  177.  
  178. /*****************************************************************************/
  179. /*****************************************************************************/
  180.  
  181.  
  182.  
  183. /* Handle cut/copy/paste for the clipboard. */
  184.  
  185. #pragma segment ClipboardDoc
  186. void    DoClipboard(FileRecHndl frHndl, short menuItem)
  187. {
  188.     FileRecHndl    frClip;
  189.     TreeObjHndl    root, rootClip, chndl;
  190.     short        cnum;
  191.     WindowPtr    clipWind, window;
  192.  
  193.     if (!(clipWind = GetNextWindow(nil, kClipboardFileType))) return;
  194.  
  195.     frClip   = (FileRecHndl)GetWRefCon(clipWind);
  196.     rootClip = (*frClip)->d.doc.root;
  197.     root     = (*frHndl)->d.doc.root;
  198.  
  199.     if (menuItem != kStdCopy)
  200.         NewDocumentUndo(frHndl);
  201.  
  202.     switch (menuItem) {
  203.         case kStdCut:
  204.         case kStdCopy:
  205.             while ((*rootClip)->numChildren) DisposeChild(NO_EDIT, rootClip, 0);
  206.                 /* First dispose of the clipboard's old content. */
  207.             for (cnum = (*root)->numChildren; cnum;) {
  208.                 chndl = GetChildHndl(root, --cnum);
  209.                 if (mDerefCommon(chndl)->selected) {    /* If document object is selected...    */
  210.                     CopyChild(NO_EDIT, root, cnum, rootClip, 0, true);            /* Copy it.     */
  211.                     mDerefCommon(GetChildHndl(rootClip, 0))->selected = false;    /* Deselect it. */
  212.                     mDerefRoot(rootClip)->numSelected = 0;
  213.                         /* Copying the child caused it to get selected, and to increment the
  214.                         ** numSelected value in the clipboard's root.  Reset it. */
  215.                 }
  216.             }
  217.             if (((WindowPeek)clipWind)->visible) {        /* If clipboard visible... */
  218.                 BeginContent(clipWind);        /* Redraw clipboard to show the new content. */
  219.                 DoImageDocument(frClip);
  220.                 EndContent(clipWind);
  221.             }
  222.             if (menuItem == kStdCut)    /* If clipboard operation was a delete... */
  223.                 DoDelete(frHndl);        /* Delete the objects out of the document. */
  224.             break;
  225.         case kStdPaste:
  226.             DoTreeSelect(root, SELECTOFF);        /* Turn off current document selections. */
  227.             for (cnum = (*rootClip)->numChildren; cnum;) {
  228.                 chndl = GetChildHndl(rootClip, --cnum);
  229.                 CopyChild(CLIPBOARD_EDIT, rootClip, cnum, root, 0, true);
  230.                     /* Copy the clipboard objects with deepCopy to copy grouped objects as well. */
  231.             }
  232.             BeginContent(window = (*frHndl)->fileState.window);        /* Redraw document. */
  233.             DoImageDocument(frHndl);
  234.             EndContent(window);
  235.             break;
  236.     }
  237. }
  238.  
  239.  
  240.  
  241.